package com.three.controller;

import com.three.filter.LoginCheckFilter;
import com.three.pojo.Result;
import com.three.servie.AvatarService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
@Slf4j
@RestController
@CrossOrigin
public class Avatar {

    @Autowired
    private AvatarService avatarService;

    @GetMapping("/avatar")
    public void getAvatar(HttpServletResponse response) throws IOException {
        String id = (String) LoginCheckFilter.getCurrentToken().get("id");
        avatarService.getAvatar(id, response);
    }

    @PostMapping("/avatar")
    public Result postAvatar(MultipartFile avatar) throws IOException {
        String id = (String) LoginCheckFilter.getCurrentToken().get("id");
        log.info("日志 POST /avatar id: " + id + "avatarName: " + avatar.getOriginalFilename());
        if (id.isEmpty()) {
            return Result.fail("用户id不能为空！");
        }
        boolean result = avatarService.postAvatar(id, avatar);
        if (result) {
            return Result.success("头像上传成功！");
        } else {
            return Result.fail("头像上传失败！");
        }
    }
}
